package com.resou.reader.bookstore.rank;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.ChannelBean;
import com.resou.reader.api.entry.RankTypeBean;
import com.resou.reader.api.entry.TypeResultBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.bookstore.rank.adapter.FragmentAdapter;
import com.resou.reader.bookstore.rank.fragment.RankBoyFragment;
import com.resou.reader.bookstore.rank.fragment.RankGrilFragment;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.view.Loading;
import com.resou.reader.view.MagincIndicatorAdapter;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 排行榜
 */
public class RankMainActivity extends ResouBaseActivity<RankPresenter> implements IRankContract.IRankView, ViewPager.OnPageChangeListener {

    /**
     * 启动排行榜页面的方法
     *
     * @param context
     */
    public static void startActivity(Context context) {
        Intent intent = new Intent(context, RankMainActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.mMagicIndicator)
    MagicIndicator mMagicIndicator;
    @BindView(R.id.rank_pager)
    ViewPager mViewPager;
    List<Fragment> mList = new ArrayList<>();
    List<ChannelBean> list = null;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    RankGrilFragment girlFragment;
    RankBoyFragment boyFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_rank_main;
    }

    @Override
    public void retryRefreshData() {

        mPresenter.quearChannel();
    }

    @Override
    protected void initStatusBar() {
    }

    @Override
    protected void initPresenter() {
        mPresenter = new RankPresenter(this);
    }

    @Override
    protected void initView() {
        tvTitle.setText("排行榜");
        list = new ArrayList<>();
        list.add(new ChannelBean(1, "男生频道"));
        list.add(new ChannelBean(2, "女生频道"));
        ClassicsFooter.REFRESH_FOOTER_NOTHING = "到底啦";

    }


    @OnClick(R.id.ivBack)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void initData() {
        mPresenter.quearChannel();
    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {

    }

    /**
     * 绑定Viewpager与mMagicIndicator
     */
    private synchronized void bindViewPager() {
        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), mList, this.list);
//        fragmentAdapter.getPageTitle(0);
        mViewPager.setAdapter(fragmentAdapter);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        mViewPager.addOnPageChangeListener(this);
        mViewPager.setCurrentItem(0);
        commonNavigator.setAdapter(new MagincIndicatorAdapter(this.list, mViewPager, true));
        mMagicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        switch (i) {
            case 0:
                boyFragment.setPageState(list.get(i).getChannelCode());
                break;
            case 1:
                girlFragment.setPageState(list.get(i).getChannelCode());
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void showChannel(List<ChannelBean> listchannel) {
        if (listchannel != null) {
            this.list = listchannel;
        }
        boyFragment = new RankBoyFragment();
        girlFragment = new RankGrilFragment();
        mList.add(boyFragment);
        mList.add(girlFragment);
        bindViewPager();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ClassicsFooter.REFRESH_FOOTER_NOTHING = "已经全部加载完毕";
    }
}
